package app.foodnote;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;
import android.support.v4.widget.SimpleCursorAdapter;
import app.foodnote.db.FoodNotesDataSource;
import app.foodnote.db.MySQLiteHelper;

public class Places_Report extends ListActivity {

	private FoodNotesDataSource mDB;
	private Cursor mCursor;
	
	Button btnReportAll;
	Button btnReport;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.places__report);
        setTitle("Thống Kê Danh Sách Ghi Chú");
        //getActionBar().setDisplayHomeAsUpEnabled(true);
        mDB = new FoodNotesDataSource(this);
        mDB.openToRead();
        
        btnReport = (Button)findViewById(R.id.Report);
        btnReportAll = (Button)findViewById(R.id.All);
        
        //Nut thong ke toan bo
        OnClickListener ReportAll=new OnClickListener() {
			
			public void onClick(View v) {				
				All();
			}
		};        
		btnReportAll.setOnClickListener(ReportAll);
		
		
		//Nut thong ke
        OnClickListener Report=new OnClickListener() {
			
			public void onClick(View v) {				
				Report();
			}
		};        
		btnReport.setOnClickListener(Report);
        
    }

    
    private void All(){    	
    	mCursor = mDB.countAll();
    	startManagingCursor(mCursor);
    	//String[] from = new String[]{MySQLiteHelper.FOOD_NAME, MySQLiteHelper.COUNT_ALL};
    	String[] from = new String[]{MySQLiteHelper.COUNT_ALL};
    	//int[] to = new int[] {R.id.foodname, R.id.address};
    	int[] to = new int[] {R.id.address};
    	@SuppressWarnings("deprecation")
		SimpleCursorAdapter users = new SimpleCursorAdapter(this, R.layout.row_all, mCursor, from, to);
    	setListAdapter(users);
    }
    
    private void Report(){    	
    	mCursor = mDB.count();
    	startManagingCursor(mCursor);
    	String[] from = new String[]{MySQLiteHelper.CITIES, MySQLiteHelper.COUNT};
    	//String[] from = new String[]{MySQLiteHelper.COUNT_ALL};
    	//int[] to = new int[] {R.id.foodname, R.id.address};
    	int[] to = new int[] {R.id.foodname, R.id.address};
    	@SuppressWarnings("deprecation")
		SimpleCursorAdapter users = new SimpleCursorAdapter(this, R.layout.row_report, mCursor, from, to);
    	setListAdapter(users);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.places__report, menu);
        return true;
    }
    

   /* 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
 */
    
}
